package cn.lf.android.lawfirm.upgrade.approval;


import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.OrdinaryBaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.distribution.modle.LawCase;
import cn.lf.android.lawfirm.undertake.approval.CaseDetailActivity;
import cn.lf.android.lawfirm.undertake.approval.UndertakeSearchListActivity;
import cn.lf.android.lawfirm.upgrade.approval.controler.UpgradeListConroller;
import cn.lf.android.lawfirm.upgrade.approval.view.UpgradeAdapter;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;

import static cn.lf.android.lawfirm.MyApplication.context;
import static cn.lf.android.lawfirm.R.id.img_left;
import static cn.lf.android.lawfirm.R.id.rl_search;

public class UpgradeListActivity extends OrdinaryBaseActivity
        implements UpgradeListConroller.UpgradeListCallback {
    private ListView mListView;
    private SmartRefreshLayout refreshLayout;
    private UpgradeAdapter mChangeUndertakeAdapter;
    private List<LawCase> list = new ArrayList<LawCase>();

    private UpgradeListConroller mChangeUnderListConroller;
    private int pageIndex = 1;
    /**
     * 待审批（notYet）、已审批（pass）、已退回（sendBack）和全部（all）（必填默认为待审批）（必填）
     **/
    private String state = "notYet";

    public static Boolean isRefresh = false;

    @Override
    public int getChildView() {
        return R.layout.activity_undertake_list;
    }

    @Override
    public void initViewId() {
        initTitle();
        refreshLayout = (SmartRefreshLayout) findId(R.id.refreshLayout);
        mListView = (ListView) findId(R.id.lv_list);
        mChangeUndertakeAdapter = new UpgradeAdapter(this, list, 0);
        mListView.setAdapter(mChangeUndertakeAdapter);
        tvSearch.setVisibility(View.VISIBLE);
        mSearch.setVisibility(View.GONE);
        setLisenters();
    }

    @Override
    public void refreshLoad() {

    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case img_left:
                finish();
                break;
            case rl_search:
                Intent intent = new Intent(this, UpgradeSearchListActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }


    private void initTitle() {
        mImgLeft.setVisibility(View.VISIBLE);
        mImgLeft.setOnClickListener(this);
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText("增级审批");
    }

    private void setLisenters() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(0);//0秒
                pageIndex = 1;
                list.clear();
                mChangeUnderListConroller.onUnderList("", String.valueOf(pageIndex), state);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
                pageIndex++;
                mChangeUnderListConroller.onUnderList("", String.valueOf(pageIndex), state);
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                Intent intent = new Intent(UpgradeListActivity.this, CaseDetailActivity.class);
//                intent.putExtra("caseID", list.get(position).caseID);
//                startActivity(intent);
            }
        });
    }

    @Override
    public void initData() {
        getData();
        SPUtil spUtil = new SPUtil(context);
        DebugLog.e("Authorization---" + spUtil.getAccessToken());
        mChangeUnderListConroller = new UpgradeListConroller(this, this);
        mChangeUnderListConroller.onUnderList("", String.valueOf(pageIndex), state);
    }


    private void getData() {
    }

    @Override
    public void onUpgradeListuccessed(List<LawCase> beans) {
        list.addAll(beans);
        mChangeUndertakeAdapter.notifyDataSetChanged();
        if (list.size() == 0) {
            noData2.setVisibility(View.VISIBLE);
        } else {
            noData2.setVisibility(View.GONE);
        }
    }

    @Override
    public void onUpgradeListFailed() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isRefresh) {
            if (null != refreshLayout) {
                refreshLayout.autoRefresh();
                isRefresh = false;
            }
        }
    }
}

